//
//  OpenHongBaoView.m
//  OpenHongBao
//
//  Created by MC on 2018/3/19.
//  Copyright © 2018年 MC. All rights reserved.
//

#import "OpenHongBaoView.h"

#define W_HongBaoView  252
#define H_HongBaoView  352
#define W_TypeView     64
#define H2  55
#define H3  240
@interface OpenHongBaoView ()


@property (nonatomic,strong) UIImageView * logoImgV;
@property (nonatomic,retain) UIView *alertView;
@property (nonatomic,retain) UIButton *dissBtn;
@property (nonatomic,retain) UIButton *sendBtn;

@property (nonatomic,retain)UILabel * lab1;//【sun***】给你发了一个红包
@property (nonatomic,retain)UILabel * lab2;//恭喜发财！大吉大利！

@property (nonatomic,strong)CAShapeLayer * redLayer;
@property (nonatomic,strong)CAShapeLayer * lineLayer;

@end


@implementation OpenHongBaoView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    self.backgroundColor = [UIColor clearColor];
    
    self.alertView = [[UIView alloc] init];
    self.alertView.backgroundColor = [UIColor clearColor];
    self.alertView.layer.cornerRadius = 5.0;

    self.alertView.layer.position = self.center;
    [self addSubview:self.alertView];
    [self.alertView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self);
    }];
    
    [self createHongBao];
    
    [self createDismissBtn];
    
    [self createLab];

}

-(NoMoneyHongBaoView *)noMoneyHongBaoView{
    if (!_noMoneyHongBaoView) {
        _noMoneyHongBaoView = [[NoMoneyHongBaoView alloc]init];
    }
    return _noMoneyHongBaoView;
}

-(OverdueHongBaoView *)overdueHongBaoView{
    if (!_overdueHongBaoView) {
        _overdueHongBaoView = [[OverdueHongBaoView alloc]init];
    }
    return _overdueHongBaoView;
}

-(SuccessHongBaoView *)successHongBaoView{
    if (!_successHongBaoView) {
        _successHongBaoView = [[SuccessHongBaoView alloc]init];
    }
    return _successHongBaoView;
}
-(void)showResultWithType:(Type_HongBaoType)Type{
    
    [_lab1 removeFromSuperview];
    [_lab2 removeFromSuperview];
    [_sendBtn removeFromSuperview];
    [_logoImgV removeFromSuperview];
    
    UIBezierPath *newPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, W_HongBaoView, H2) byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(4, 4)];
    CGPoint startPoint = CGPointMake(0,  H2);
    CGPoint endPoint = CGPointMake(W_HongBaoView, H2);
    CGPoint controlPoint = CGPointMake(W_HongBaoView/2.0, H2+20);
    
    //曲线起点
    [newPath moveToPoint:startPoint];
    //曲线终点和控制基点
    [newPath addQuadCurveToPoint:endPoint controlPoint:controlPoint];
    
    CGRect newFrame = CGRectMake(0, 0, W_HongBaoView, H2);
    
    CABasicAnimation* pathAnim = [CABasicAnimation animationWithKeyPath: @"path"];
    pathAnim.toValue = (id)newPath.CGPath;
    
    CABasicAnimation* boundsAnim = [CABasicAnimation animationWithKeyPath: @"frame"];
    boundsAnim.toValue = [NSValue valueWithCGRect:newFrame];
    
    CAAnimationGroup *anims = [CAAnimationGroup animation];
    anims.animations = [NSArray arrayWithObjects:pathAnim, boundsAnim, nil];
    anims.removedOnCompletion = NO;
    anims.duration = 0.1f;
    anims.fillMode  = kCAFillModeForwards;
    [self.lineLayer addAnimation:anims forKey:nil];
    
    [_redLayer setFillColor:RGB(255, 255, 255).CGColor];
    
    
    if (Type==HongBaoType_Success) {
        [self addSubview:self.successHongBaoView];
        [self.successHongBaoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(55);
            make.left.right.bottom.equalTo(self);
        }];

        [self.successHongBaoView.lookRecordBtn addTarget:self action:@selector(lookRecord) forControlEvents:UIControlEventTouchUpInside];
        
    }else if(Type==HongBaoType_Overdue){
        [self addSubview:self.overdueHongBaoView];
        [self.overdueHongBaoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(55);
            make.left.right.bottom.equalTo(self);
        }];

    }else if(Type==HongBaoType_NoMoney){
        [self addSubview:self.noMoneyHongBaoView];
        [self.noMoneyHongBaoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(55);
            make.left.right.bottom.equalTo(self);
        }];
    
    }
}

-(void)createLab{
    
    _logoImgV = [[UIImageView alloc]init];
    [self addSubview:_logoImgV];
    _logoImgV.layer.cornerRadius=25;
    _logoImgV.clipsToBounds=YES;
    [_logoImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.mas_top).offset(57);
        make.height.width.mas_equalTo(50);
    }];
    
    _lab1=[[UILabel alloc]init];
    [self addSubview:_lab1];
    _lab1.textAlignment = NSTextAlignmentCenter;
    _lab1.font = [UIFont systemFontOfSize:9];
    _lab1.textColor=RGB(255,173,167);
    [_lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.mas_top).offset(110);
        make.right.equalTo(self.mas_right).offset(-10);
        make.height.mas_equalTo(20);
    }];
    
    _lab2=[[UILabel alloc]init];
    [self addSubview:_lab2];
    _lab2.textAlignment = NSTextAlignmentCenter;
    _lab2.font = [UIFont systemFontOfSize:15];
    _lab2.textColor=RGB(255,226,148);
    [_lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.mas_top).offset(140);
        make.right.equalTo(self.mas_right).offset(-10);
        make.height.mas_equalTo(30);
    }];
    
}

-(void)createHongBao{

    //深色背景
    _redLayer = [[CAShapeLayer alloc] init];
    _redLayer.backgroundColor=[UIColor orangeColor].CGColor;
    UIBezierPath *pathFang = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, W_HongBaoView,H_HongBaoView) cornerRadius:4];
    _redLayer.path = pathFang.CGPath;
    _redLayer.zPosition = 1;
    [self.alertView.layer addSublayer:_redLayer];
    [_redLayer setFillColor:RGB(189, 57, 51).CGColor];//深色背景

    //浅色红包口
    _lineLayer = [[CAShapeLayer alloc] init];
    _lineLayer.backgroundColor=RGB(217, 83, 76).CGColor;

    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, W_HongBaoView, 240) byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(4, 4)];
    CGPoint startPoint = CGPointMake(0, 240);
    CGPoint endPoint = CGPointMake(W_HongBaoView, 240);
    CGPoint controlPoint = CGPointMake(120, 260);

    //曲线起点
    [path moveToPoint:startPoint];
    //曲线终点和控制基点
    [path addQuadCurveToPoint:endPoint controlPoint:controlPoint];
    //曲线部分颜色和阴影
    [_lineLayer setFillColor:[UIColor colorWithRed:0.851 green:0.3216 blue:0.2784 alpha:1.0].CGColor];
    [_lineLayer setStrokeColor:[UIColor colorWithRed:0.9401 green:0.0 blue:0.0247 alpha:0.02].CGColor];
    [_lineLayer setShadowColor:[UIColor blackColor].CGColor];
    [_lineLayer setLineWidth:0.1];
    [_lineLayer setShadowOffset:CGSizeMake(6, 6)];
    [_lineLayer setShadowOpacity:0.2];
    [_lineLayer setShadowOffset:CGSizeMake(1, 1)];
    _lineLayer.path = path.CGPath;
    _lineLayer.zPosition = 1;
    [self.alertView.layer addSublayer:_lineLayer];

    //发红包按钮
    UIButton *sendBtn = [[UIButton alloc] init];
    sendBtn.backgroundColor=[UIColor clearColor];
    sendBtn.layer.masksToBounds = YES;
    [sendBtn addTarget:self action:@selector(moveAnimation:) forControlEvents:UIControlEventTouchUpInside];
    [sendBtn setBackgroundImage:[UIImage imageNamed:@"kai"] forState:UIControlStateNormal];
    sendBtn.layer.zPosition = 3;
    [self addSubview:sendBtn];
    _sendBtn=sendBtn;
    [sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.mas_top).offset(240-50);
        make.height.width.mas_equalTo(100);
    }];
}

/*
 * 加载取消按钮
 */
-(void)createDismissBtn{
    

    _dissBtn = [[UIButton alloc] init];
    _dissBtn.backgroundColor=[UIColor clearColor];
    _dissBtn.layer.masksToBounds = YES;
    [_dissBtn addTarget:self action:@selector(ActionDismiss:) forControlEvents:UIControlEventTouchUpInside];
    [_dissBtn setBackgroundImage:[UIImage imageNamed:@"chacha"] forState:UIControlStateNormal];
    _dissBtn.layer.zPosition = 3;
    [self addSubview:_dissBtn];
    [_dissBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-13);
        make.top.equalTo(self.mas_top).offset(13);
        make.height.width.mas_equalTo(16);
    }];
}

-(void)moveAnimation:(UIButton *)sendBtn{
    
    //    </pre>
    //    添加 按钮旋转动画 需给按钮设置层级，否则旋转超过XY面时，会穿透背景_lineLayer和_redLayer

    CABasicAnimation *transformAnima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
//     transformAnima.fromValue = @(M_PI);
//    transformAnima.toValue = [NSNumber numberWithFloat: -M_PI];
    
    // 设定旋转角度
    transformAnima.fromValue = [NSNumber numberWithFloat: M_PI *2]; // 起始角度
    transformAnima.toValue = [NSNumber numberWithFloat:0.f]; // 终止角度
    
    transformAnima.duration = 0.5;
    transformAnima.cumulative = YES;
    transformAnima.autoreverses = NO;
    transformAnima.repeatCount = HUGE_VALF;
    transformAnima.fillMode = kCAFillModeForwards;
    transformAnima.removedOnCompletion = NO;
    transformAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    sendBtn.layer.zPosition = 5;
    sendBtn.layer.zPosition = sendBtn.layer.frame.size.width/2.f;
    [sendBtn.layer addAnimation:transformAnima forKey:@"rotationAnimationY"];
    
    if (self.moveAnimationHongBaoBlock) {
        self.moveAnimationHongBaoBlock();
    }
}


-(void)setDataSource:(MCCheckUserGrabModel *)dataSource{
    _dataSource = dataSource;
    
    _logoImgV.image=[UIImage imageNamed:@"hblg"];
    _lab1.text = [NSString stringWithFormat:@"【%@】给你发了一个红包",_dataSource.SendName];// @"【sun***】给你发了一个红包";
    _lab2.text =_dataSource.Name; //@"恭喜发财！大吉大利！";
}


#pragma mark-查看记录
-(void)lookRecord{
    if (self.goToDetailHongBaoBlock) {
        self.goToDetailHongBaoBlock();
    }
    [self removeFromSuperview];
}

#pragma mark-退出
- (void)ActionDismiss:(UIButton *)sender
{
    if (self.dismissblock) {
        self.dismissblock();
    }
    [self removeFromSuperview];
}
@end


#pragma mark-成功
@interface  SuccessHongBaoView ()
@property (nonatomic,strong)UIImageView * resultLogo;
@property (nonatomic,strong)UILabel * moneyLab;
@property (nonatomic,strong)UILabel * remarkLab;

@end


@implementation SuccessHongBaoView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}

-(void)createUI{
    UIImageView * resultLogo = [[UIImageView alloc]init];
    [self addSubview:resultLogo];
    resultLogo.image=[UIImage imageNamed:@"duihao"];
    _resultLogo=resultLogo;
    [resultLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(80-55);
        make.centerX.equalTo(self.mas_centerX);
        make.width.height.mas_equalTo(W_TypeView);
    }];
    
    _moneyLab=[[UILabel alloc]init];
    [self addSubview:_moneyLab];
//    _moneyLab.text = @"88.88";
    _moneyLab.textAlignment = NSTextAlignmentCenter;
    _moneyLab.font = [UIFont systemFontOfSize:30];
    _moneyLab.textColor=RGB(40,40,40);
    [_moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.mas_top).offset(160-55);
        make.right.equalTo(self.mas_right).offset(-10);
        make.height.mas_equalTo(35);
    }];
    
    _remarkLab=[[UILabel alloc]init];
    [self addSubview:_remarkLab];
//    _remarkLab.text = @"【sun***】给你发了一个红包";
    _remarkLab.textAlignment = NSTextAlignmentCenter;
    _remarkLab.font = [UIFont systemFontOfSize:9];
    _remarkLab.textColor=RGB(204,204,204);
    [_remarkLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.mas_top).offset(195-55);
        make.right.equalTo(self.mas_right).offset(-10);
        make.height.mas_equalTo(35);
    }];
    
    
    _lookRecordBtn = [[UIButton alloc] init];
    _lookRecordBtn.backgroundColor=[UIColor clearColor];
    _lookRecordBtn.layer.masksToBounds = YES;
    _lookRecordBtn.titleLabel.font=[UIFont systemFontOfSize:14];
    [_lookRecordBtn setTitle:@"查看详情" forState:UIControlStateNormal];
    [_lookRecordBtn setTitleColor:MC_THEME_COLOR forState:UIControlStateNormal];
    [self addSubview:_lookRecordBtn];
    [_lookRecordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.mas_top).offset(240-55);
        make.right.equalTo(self.mas_right).offset(-10);
        make.height.mas_equalTo(30);
    }];
    
}

-(void)relayOutUIWithMoney:(NSString *)money andRemark:(NSString *)remark{
    
    _moneyLab.text = money;
    _remarkLab.text =remark;// @"【sun***】给你发了一个红包";
    
}

-(void)setDataSource:(id)dataSource{
    _dataSource=dataSource;
//    _moneyLab.text = @"88.88";
//    _remarkLab.text = @"【sun***】给你发了一个红包";

}
@end

#pragma mark-过期
@interface  OverdueHongBaoView ()
@property (nonatomic,strong)UIImageView * resultLogo;
@property (nonatomic,strong)UILabel * moneyLab;
@property (nonatomic,strong)UILabel * remarkLab;

@end


@implementation OverdueHongBaoView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    UIImageView * resultLogo = [[UIImageView alloc]init];
    [self addSubview:resultLogo];
    resultLogo.image=[UIImage imageNamed:@"guoqi"];
    _resultLogo=resultLogo;
    [resultLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.mas_top).offset(80-55);
        make.centerX.equalTo(self.mas_centerX);
        make.width.height.mas_equalTo(W_TypeView);
    }];
    
    _moneyLab=[[UILabel alloc]init];
    [self addSubview:_moneyLab];
    _moneyLab.textAlignment = NSTextAlignmentCenter;
    _moneyLab.font = [UIFont systemFontOfSize:15];
    _moneyLab.textColor=RGB(102,102,102);
    [_moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.mas_top).offset(160-55);
        make.right.equalTo(self.mas_right).offset(-10);
        make.height.mas_equalTo(25);
    }];
    
    _remarkLab=[[UILabel alloc]init];
    [self addSubview:_remarkLab];
    _remarkLab.textAlignment = NSTextAlignmentCenter;
    _remarkLab.font = [UIFont systemFontOfSize:9];
    _remarkLab.textColor=RGB(204,204,204);
    [_remarkLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.mas_top).offset(195-55);
        make.right.equalTo(self.mas_right).offset(-10);
        make.height.mas_equalTo(35);
    }];
}

-(void)setDataSource:(id)dataSource{
    
    _dataSource=dataSource;
    _moneyLab.text = @"红包已过期！";
    _remarkLab.text = @"该红包超过24小时未领取。";
    
}

@end


#pragma mark-抢光了
@interface  NoMoneyHongBaoView ()
@property (nonatomic,strong)UIImageView * resultLogo;
@property (nonatomic,strong)UILabel * moneyLab;
@property (nonatomic,strong)UILabel * Tip;//消费满5555才可以领取喔

@end


@implementation NoMoneyHongBaoView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    UIImageView * resultLogo = [[UIImageView alloc]init];
    [self addSubview:resultLogo];
    resultLogo.image=[UIImage imageNamed:@"tanhao"];
    _resultLogo=resultLogo;
    [resultLogo mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.mas_top).offset(80);
        make.centerX.equalTo(self.mas_centerX);
        make.width.height.mas_equalTo(W_TypeView);
    }];
    
    _moneyLab=[[UILabel alloc]init];
    [self addSubview:_moneyLab];
    _moneyLab.textAlignment = NSTextAlignmentCenter;
    _moneyLab.font = [UIFont systemFontOfSize:15];
    _moneyLab.numberOfLines=0;
    _moneyLab.textColor=RGB(102,102,102);
    [_moneyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.mas_top).offset(165);
        make.right.equalTo(self.mas_right).offset(-10);
//        make.height.mas_equalTo(25);
    }];
    
    
    _Tip=[[UILabel alloc]init];
    [self addSubview:_Tip];
    _Tip.textAlignment = NSTextAlignmentCenter;
    _Tip.font = [UIFont systemFontOfSize:15];
    _Tip.textColor=RGB(102,102,102);
    [_Tip mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.mas_top).offset(165+25);
        make.right.equalTo(self.mas_right).offset(-10);
        make.height.mas_equalTo(25);
    }];
}

-(void)setDataSource:(NSString *)dataSource{
    _dataSource=dataSource;
    _moneyLab.text = dataSource;//@"手慢了，红包已被抢光！";

}
-(void)setStandardAmount:(NSString *)standardAmount{
    _standardAmount=standardAmount;
    if(standardAmount.length<1){
        return;
    }
//     （消费≥x元 & 充值≥x元才可领取喔）
    _Tip.text = standardAmount;//消费满5555才可以领取喔
}

@end
















